<template>
  <div class="login">
    <div class="logo">
      <img src="../assets/img/logo.png" alt="">
    </div>
    <div class="logn_back">

    </div>
    <div class="from">
      <!-- 上方切换栏 -->
      <div class="tab">
        <div class="tab-title">
          <div class="tab-bold">账号登录</div>
          <div>手机号登录</div>
        </div>
        <div class="QRcode" @click="code">
          <img src="../assets/img/二维码.png" alt="">
        </div>
      </div>
      <!-- 表单登录 -->
      <div class="el-from">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账户" prop="username" label-width="70px">
            <el-input type="text" v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass" label-width="70px">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off" @keyup.native.enter="keydown"></el-input>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="checked" @change="checkbox_change">记住账号</el-checkbox>
          </el-form-item>
          <el-form-item label-width="150px">
            <el-button type="primary" :plain="true" @click="submitForm('ruleForm')">点击登录</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 注册 -->
      <div class="enroll">
        <el-link @click="message">没有账号? 点我注册</el-link>
        <el-link>找回密码</el-link>
      </div>
    </div>

  </div>
</template>

<script>
import { getUser } from '../config/api'

export default {
  data() {
    var userName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'))
      }
      callback()
    }
    var password = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }
      if (new String(value).length < 6) {
        callback(new Error('最少6位密码'))
      }
      callback()
    }
    return {
      Base64: require('js-base64').Base64,
      checked: false,
      ruleForm: {
        pass: '',
        username: ''
      },
      rules: {
        pass: [{ validator: password, trigger: 'blur' }],
        username: [{ validator: userName, trigger: 'blur' }]
      }
    }
  },

  methods: {
    code() {
      // console.log('code')
    },
    // 登录按钮
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.GetUser()
        } else {
          if (this.ruleForm.pass.length < 6) {
            this.$message({
              showClose: true,
              message: '请输入6位以上密码',
              type: 'warning'
            })
            return false
          }
          this.$message({
            showClose: true,
            message: '不允许为空哦',
            type: 'warning'
          })
          return false
        }
      })
    },
    // 监听回车
    keydown(e) {
      if (e.target.value == '') return
      if (this.ruleForm.pass.length < 6) return
      this.GetUser()
    },

    // 登录请求
    GetUser() {
      getUser(this.ruleForm.username, this.ruleForm.pass)
        .then(result => {
          // console.log(result.data)
          if (result.status == 200) {
            if (result.data.code == 200) {
              // 保存用户名
              if (this.checked) {
                window.localStorage.setItem('uName', this.Base64.encodeURI(result.data.data.username))
              } else {
                window.localStorage.removeItem('uName')
              }
              // console.log(Base64.decode())

              // 保存Tokan
              window.localStorage.setItem('shopping_token', this.Base64.encodeURI(result.data.token))
              // 保存用户id
              window.localStorage.setItem('userId', result.data.data.userId)
              // 是否非法进入  123为已登录
              window.localStorage.setItem('_shopping_islogin', '123')

              // 正确
              this.$message({
                showClose: true,
                message: '登录成功',
                type: 'success'
              })
              // 保存用户登录信息  0 登录  1 登录信息过期
              this.$Cookie.setItem('login', '0', 60000)
              this.$router.push({
                path: '/home'
              })
            }
            if (result.data.code == 404) {
              this.$message({
                showClose: true,
                message: result.data.msg,
                type: 'error'
              })
            }
          }
        })
        .catch(err => {
          if (err.code == 'ERR_BAD_REQUEST') {
            alert('请求错误')
          }
        })
    },
    // 单选按钮
    checkbox_change(e) {
      // 0为未记录 1为记录
      if (e) {
        window.localStorage.setItem('shopping_isRecordUser', '1')
      } else {
        window.localStorage.setItem('shopping_isRecordUser', '0')
      }
    },

    message(e) {}
  },
  mounted() {
    // 数值型：0 为false 1 为true
    this.checked = Boolean(Number(window.localStorage.getItem('shopping_isRecordUser')))

    // 读取账户
    if (window.localStorage.getItem('uName') == null) return
    this.ruleForm.username = this.Base64.decode(window.localStorage.getItem('uName'))
  }
}
</script>

<style lang="less" scoped>
.login {
  // 隐藏横向滚动条
  // overflow-x: hidden;
  position: relative;
  width: 100%;
  height: 100vh;
  background-image: linear-gradient(120deg, #f6d365 0%, #fda085 100%);
  .logo {
    width: 1200px;
    margin: 0 auto;
    img {
      width: 200px;
      height: 100px;
      border-radius: 20px;
    }
  }
  // 背景图片
  .logn_back {
    margin-top: 100px;
    width: 100%;
    height: 600px;
    position: absolute;
    top: 0;
    z-index: 0;
    opacity: 0.6;
    background: no-repeat center/100% url(../assets/img/login_img.jpg);
  }
  .from {
    &::before {
      content: '';
      width: 400px;
      height: 5px;
      position: absolute;
      top: -5px;
      left: 0px;
      background-color: #ed9e59;
    }
    position: absolute;
    top: 200px;
    right: 50px;
    width: 400px;
    height: 400px;
    margin: 0 auto;
    background-color: #fff;
    .tab {
      display: flex;
      justify-content: flex-end;
      .tab-title {
        margin-top: @login_size_10px + 30;
        font-size: 18px;
        display: flex;
        margin-right: (400 / 4px) - 70px;
        div {
          // 过渡 时间1秒
          transition: 1s;
        }
        .tab-bold {
          font-weight: bold;
          // 放大 自己的1.15倍
          transform: scale(1.15);
        }
      }
      div {
        margin-left: @login_size_10px + 10px;
        cursor: pointer;
      }
    }
    // 二维码样式
    .QRcode {
      position: relative;
      img {
        width: 50px;
        margin-right: @login_size_10px;
        margin-top: 5px;
      }
      &:hover::before {
        display: block;
      }
      &::before {
        content: '扫码登录更方便';
        position: absolute;
        top: 2px;
        left: -100px;
        font-size: 12px;
        border: 2px solid #000;
        border-radius: 20px;
        padding: 3px;
        background-color: #ffffff;
        display: none;
      }
    }
    .el-from {
      margin-top: 50px;
      .el-form-item {
        margin-bottom: 20px;
        margin-right: 20px;
      }
      // 记住账号
      .el-form-item:nth-child(3) {
        text-align: right;
      }
    }
    // 注册
    .enroll {
      margin-top: 30px;
      display: flex;
      justify-content: flex-end;
      a {
        margin-right: @login_size_10px;
      }
    }
  }
}
</style>